package server.game;

import java.util.ArrayList;
import java.util.Iterator;

import server.controller.ClientSession;
import server.controller.OutboundMessages;
import server.util.Logger;
import shared.messages.StartMessage;
import shared.player.Player;
import shared.util.Config;

/**
 * The Class GameStarter
 *  - initializes a new game and sends the required 
 *  information to the clients.
 */
public class GameStarter extends Thread {

	/** The list of clients for the new game. */
	ArrayList<ClientSession> inGame = new ArrayList<ClientSession>();
	
	/** The outboundMessages object. */
	OutboundMessages outQueue;
	
	/** The game id, increments by 1 with each new game created. */
	int gameId;
	
	/** The id of the player to be created to the game next. */
	int playerCount = 1;
	
	/** The players. */
	Player p1, p2, p3;
	
	/** The gameboard. */
	Integer[][] board;
	
	/** The logger for logging error and server messages. */
	Logger logger;
	
	/** The configuration object. */
	Config config;

	/**
	 * Instantiates a new game starter.
	 *
	 * @param forGame the list of clientsessions for this game
	 * @param outQueue the outboundMessages object
	 * @param gameId the games id
	 * @param logger the logger for loggin server and error messages
	 * @param config the configuration object
	 */
	public GameStarter(ArrayList<ClientSession> forGame, OutboundMessages outQueue,
			int gameId, Logger logger, Config config) {
		//create a clone of the objects inside
		inGame.addAll(forGame);
		this.outQueue = outQueue;
		this.gameId = gameId;
		this.logger = logger;
		this.config = config;
		start();
	}

	/** Creates the players and starts the game
	 * @see java.lang.Thread#run()
	 */
	public void run(){
		Iterator<ClientSession> itr = inGame.iterator();
		while(itr.hasNext()){
			ClientSession cli = (ClientSession) itr.next();
			switch (playerCount){
			case 1:
				p1 = cli.p = new Player(cli.getName(), 10, 50, 3, gameId, playerCount);
				break;
			case 2:
				p2 = cli.p = new Player(cli.getName(), 50, 90, 0, gameId, playerCount);
				break;
			case 3:
				p3 = cli.p = new Player(cli.getName(), 90, 50, 2, gameId, playerCount);
			}
			playerCount++;
			if(playerCount == 4){
				board = getNewBoard();
				outQueue.addMessage(new StartMessage(gameId, p1, p2, p3));
				break;
			}
		}
		while(true){
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				logger.logError("GameStart - run() - sleep interrupted.");
			}
			if(checkReady()){
				logger.log("Game " + gameId + " started.");
				new GameController(outQueue, gameId, p1, p2, p3, board, logger, config);
				break;
			}
		}
	}

	/**
	 * Check if all players are ready to start the game.
	 *
	 * @return true, if successful
	 */
	public boolean checkReady() {
		Iterator<ClientSession> itr = inGame.iterator();
		while (itr.hasNext()) {
			ClientSession cli = (ClientSession) itr.next();
			if (!cli.getReady()) {
				return false;
			}
		}
		return true;
	}
	
	/**
	 * Gets the new gameboard.
	 *
	 * @return the new gameboard
	 */
	public Integer[][] getNewBoard(){
		Integer[][] board = new Integer[100][100];
		for (int i = 0; i < 100; i++) {
			for (int j = 0; j < 100; j++) {
				board[i][j] = 0;
			}
		}
		for (int i = 0; i < 100; i++) {
			board[0][i] = 1;
			board[99][i] = 1;
			board[i][0] = 1;
			board[i][99] = 1;
		}
	return board;
	}
}
